import { IValidator } from "@/controlor/Validator/IValidator";
import { ValidatorResponse } from "@/Types";

/**
 * 验证字符串数字在指定范围内
 *
 * @param min 下边界
 * @param max 上边界
 * @param includeMin 是否包括上边界
 * @param includeMax 是否包括下边界
 * @param err 验证失败时输出的字符串
 *
 * @exmaple
 *
 */
export const StringNumBetweenValidator = function(
  min: number,
  max: number,
  includeMin = false,
  includeMax = false,
  err = `输入数据不在${includeMin ? "[" : "("}${min},${max}${
    includeMax ? "]" : ")"
  }内。`
): IValidator<string> {
  return function(input: string): ValidatorResponse {
    const num = Number(input);

    if (includeMin && num < min) {
      return err;
    } else if (num <= min) {
      return err;
    }
    if (includeMax && num > max) {
      return err;
    } else if (num >= max) {
      return err;
    }

    return true;
  };
};
